
$(document).ready(function() {

        //log('editAttributes.js: document.ready')

        // Ukoliko uredjujemo objekat, kroz ucitani html dosupne su promjenjljive:        
        // var editObjectDialog_object_type = '<?=$object['type']?>';
        // var editObjectDialog_owner = '<?=$object['owners_ID']?>';
        // var editObjectDialog_post_ID = '<?=$object['ID']?>';

        newAttributeDialog = function(url)
        {
            var url = url;
            var dialogClass = 'addAttributeDialog';
            var dialogTitle = 'New Attribute';
            var formToSubmit = '#newAttributeDialog_form';
            var buttons = [
                {
                    id: 'addNewAttribute',
                    text: "OK",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];            
            openNewDialogOnOpen = function() {

                // Override generation of error label
                jQuery(formToSubmit).validate({

                        /*rules: {
                            newAttributeDialog_value: { check_item_dropdown: true },
                            newAttributeDialog_name: "required"  
                        }, */
                        highlight: function(element) {
                            $(element).parent().parent().addClass("error");
                        },
                        unhighlight: function(element) {
                            $(element).parent().parent().removeClass("error");
                        },
                        submitHandler: function(form) {

                            //disable add button                            
                            $('#'+buttons[0].id).button("option", "disabled", true);

                            $('.'+dialogClass+' .status_info').html(loader_html());


                            var form = $(formToSubmit);
                            var form_data = form.serialize();

                            $.ajax({
                                    url: form.attr('action'),
                                    dataType: 'json',
                                    type: "POST",
                                    data: form_data,
                                    success: function (data, textStatus, xhr) {

                                        if(data.action==true){

                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'success'));

                                            $('#attrToObjectDialog_attributes_ID').append(
                                                $('<option></option>').val(data.id).html($('#newAttributeDialog_name').val())
                                                );

                                            $('#attrToObjectDialog_attributes_ID').val(data.id)

                                            function action_delay(){
                                                $('.'+dialogClass).dialog("close");  
                                            }
                                            window.setTimeout(function() { action_delay(); }, 1001);

                                        }else{

                                            $('#'+buttons[0].id).button("option", "disabled", false);
                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'error'));
                                        }
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        //enable add button                            
                                        $('#'+buttons[0].id).button("option", "disabled", false); 
                                        $('.'+dialogClass+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                    }
                            });

                        },
                        errorPlacement: function(error, element){}

                })

                //log(myObject[0])

            }
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,567,550);
        }

})